//
//  HotClassDetailVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/9/19.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "HotClassDetailVC.h"
#import "FindLayout.h"
#import "ArtGalleryCell_third.h"
#import "IGDatabaseApi.h"
#import "IGDatabaseDetailApi.h"
#import "XiangQingViewController.h"
@interface HotClassDetailVC ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
{
    UICollectionView *collect;
    NSString *pageNumber;//页数，从0开始
    NSString *pageSize;//一页显示列表数
    NSString *queryTime;//查询时间
}
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)IGDatabaseDetailApi *api;

@end

@implementation HotClassDetailVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadUI];
    pageNumber = @"0";
    pageSize = @"10";
    queryTime = @"";
    self.api = [IGDatabaseDetailApi new];
    self.dataArr = [@[] mutableCopy];
    [self dataRequest];
}
- (void)loadUI
{
    FindLayout *layout = [[FindLayout alloc] init];
    collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:layout];
    collect.backgroundColor = RGB(234, 234, 234);
    collect.alwaysBounceVertical = YES;
    collect.delegate = self;
    collect.dataSource = self;
    [collect registerClass:[ArtGalleryCell_third class] forCellWithReuseIdentifier:@"ArtGalleryCell_third"];
    @KWeakObj(self)
    [collect ig_setHeaderWithRefreshingBlock:^{
        pageNumber = @"0";
        pageSize = @"10";
        queryTime = @"";
        [selfWeak dataRequest];
    }];
    [self.view addSubview:collect];
}


- (void)dataRequest
{
    @KWeakObj(self)
    selfWeak.api.type = _type;
    selfWeak.api.value = _value;
    selfWeak.api.pageNumber = pageNumber;
    selfWeak.api.pageSize = pageSize;
    selfWeak.api.queryTime = queryTime;
    [self.api startWithDecodeClass:[IGDatabaseDetailResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [collect ig_endHeaderRefreshing];
        [collect ig_endFooterRefreshing];
        if (responseObject)
        {
            IGDatabaseDetailResp *resp = responseObject;
            IGDatabaseDetailModel *model = resp.data;
            if (model.lstWork && model.lstWork.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [collect ig_endFooterRefreshingWithNoMoreData];
            }
            queryTime = model.queryTime;
            
            if ([pageNumber isEqualToString:@"0"])
            {
                _dataArr = [NSMutableArray arrayWithArray:model.lstWork];
            }
            else
            {
                [_dataArr addObjectsFromArray:model.lstWork];
            }
            [collect reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
//            [selfWeak presentMessageTips:@"无内容"];
            [selfWeak presentMessageTips:NSLocalizedString(@"No Content", nil)];
        }
    }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [collect ig_setFooterWithRefreshingBlock:^{
        pageNumber = [NSString stringWithFormat:@"%ld", [pageNumber integerValue] + 1];
        [selfWeak dataRequest];
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count?:0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ArtGalleryCell_third *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtGalleryCell_third" forIndexPath:indexPath];
    IGArtWorkModel *base = _dataArr[indexPath.item];
    cell.imgUrl = base.image;
    cell.titleL = base.title;
    [cell setNeedsLayout];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel *base = _dataArr[indexPath.item];
    XiangQingViewController *vc = [[XiangQingViewController alloc] init];
    vc.p_id = base.aId;
    vc.isRoot = YES;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel *base = _dataArr[indexPath.item];
    if ([base.plates integerValue]==1)
    {
        //横板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1080/1920));
    }
    else if ([base.plates integerValue]==2)
    {
        //坚板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1920/1080));
    }
    return CGSizeMake(0, 0);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
